const ReportService = require('../services/report.service')

const reportService = new ReportService()

class ReportController {
  async findAll (ctx, next) {
    const query = ctx.request.query
    if (ctx.state.user.role === 'reporter') {
      query.submitter = ctx.state.user._id
    }
    if (query.state) {
      query.unpassedItems = query.state === 'report_normal' ? [] : { $gt: [] }
    }
    const data = await reportService.findAll(query)
    ctx.response.body = data
  }
  async findOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await reportService.findOne(id)
    ctx.response.body = data[0]
  }
  async updateOne (ctx, next) {
    const id = ctx.request.params.id
    const body = ctx.request.body
    const data = await reportService.updateOne(id, body)
    ctx.response.body = data
  }
  async deleteOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await reportService.deleteOne(id)
    ctx.response.body = data
  }
  async addOne (ctx) {
    const body = ctx.request.body
    body.submitter = ctx.state.user._id
    const data = await reportService.addOne(body)
    ctx.response.body = data
  }
}

module.exports = ReportController